set -e

echo "软件源替换为网易源"
sed -i 's/archive.ubuntu.com/mirrors.163.com/g' /etc/apt/sources.list
sed -i 's/security.ubuntu.com/mirrors.163.com/g' /etc/apt/sources.list
apt update -y
#apt upgrade -y

echo "安装用到的软件"
apt install -y wget vim net-tools gcc libc-dev zip unzip build-essential

echo "解压 jdk8u41-src.zip 到 /usr/src/openjdk"
unzip -q /vagrant/resources/jdk8u41-src.zip -d /usr/src

echo "安装 jdk7 作为编译 jdk8 的 bootstrap JDK"
echo "解压 jdk7u75-bin.tar.gz 到 /usr/lib/jdk/7u75"
mkdir -p /usr/lib/jdk/7u75
# -C 表示解压指定目录
# --strip-components N 表示删除目录结构（n=1表示删除最外层结构）
tar -xzf /vagrant/resources/jdk7u75-bin.tar.gz -C /usr/lib/jdk/7u75 --strip-components 1

#echo "设置 jdk7u75 环境变量"
#cat > /etc/profile.d/java.sh << EOF
#export JAVA_HOME=/usr/lib/jdk/7u75
#export PATH=\$JAVA_HOME/bin:\$PATH
#EOF

echo "安装编译jdk依赖的库"
apt install -y \
    alsa libfreetype6 libfreetype6-dev cups libxrender-dev libxext-dev libxrender-dev libxtst-dev \
    libxt-dev libcups2-dev libasound2-dev ccache

echo "检查编译环境"
cd /usr/src/openjdk
bash ./configure --with-boot-jdk=/usr/lib/jdk/7u75 --with-freetype-include=/usr/include/freetype2 --with-freetype-lib=/usr/lib/x86_64-linux-gnu

echo "开始编译JDK"
make DISABLE_HOTSPOT_OS_VERSION_CHECK=ok all
